<?php
class ManagerController extends Controller {
        
    function __construct() {
        
        if (isset($_SESSION['userID'])) {
            $login = new Login();
            if (!$login->get($_SESSION['userID']))
                header("Location: ".SITE_URL.'?c=Login');
        }
        else {
            header("Location: ".SITE_URL.'?c=Login');
        }
        
        parent::__construct();
        
    }
    
	function index() {
            
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            $this->vars['section'] = "Manager";
            $this->vars['title'] = "List of Managers";
            
            $array_menus = array();
        
            $filter = array();
            $filter['parent_id'] = 0;
            $filter['type'] = 1;
            Menu::getMenuList($array_menus, $filter);
            
            $this->vars['array_menus'] = $array_menus;
            
            $member = new Login();
            
            Login::
            
            $member->addSelect();
            $member->addSelect("question.id question_id,
                                    question.title question_title,
                                    answer.id answer_id,
                                    answer.content answer_content,
                                    answer.is_correct answer_correct");
            $member->addJoin(new Answer(), 'LEFT');
            $member->addWhere("question_type_id = 1");
            $member->orderBy("question.title ASC, answer.content ASC");
            $member->find();
            
            $this->vars['question'] = $member;
            
            $this->display('GetThePicture_list');
        }
        
        function uploadPicture($name, $overwrite = false) {
            if ((($_FILES["image"]["type"] == "image/gif")
                || ($_FILES["image"]["type"] == "image/jpeg")
                || ($_FILES["image"]["type"] == "image/pjpeg"))
                && ($_FILES["image"]["size"] < 1000000))
            {
                
                $extension = explode(".", $_FILES["image"]["name"]);
                $extension = $extension[count($extension) - 1];
                
                if ($_FILES["image"]["error"] > 0) {
                    MessageHandler::add("Return Code: " . $_FILES["image"]["error"]);
                }
                else {
                    
                    if (file_exists(APPLICATION_PATH."/../images/" . $name.'.'.$extension) && !$overwrite)
                    {
                        MessageHandler::add("The file is already existed.");
                    }
                    else {
                        move_uploaded_file($_FILES["image"]["tmp_name"], APPLICATION_PATH."/../images/" . $name.'.'.$extension);
                    }
                }
            }
            else
            {
                MessageHandler::add("Invalid file");
            }
            
            if (MessageHandler::countError() > 0)
                return false;
            else
                return $name.'.'.$extension;
        }
        
        function addQuestion() {
            
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            $cfer = new Cfer(array(
            _LB_QUESTION => SITE_URL.'?c=GetThePicture',
            _LB_ADD_QUESTION => SITE_URL.'?c=GetThePicture&a=addQuestion'));
            
            $this->vars['section'] = "GetThePicture";
            $this->vars['title'] = "Add a question of 'Get The Picture'";
            $this->vars['backlink'] = SITE_URL.'?c=GetThePicture';
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $member = new Question();
            
            if ($act == ACT_VALID_EDIT) {
                
                $member->title = isset($_POST['title']) ? $_POST['title'] : '';
                $member->question_type_id = 1;
                
                if ($member->validateInput()) {
                    
                    $id = $member->insert();
                    
                    if ($id) {
                        if ($file = $this->uploadPicture("1_".$id)) {
                            $member = new Question();
                            $member->get($id);
                            $member->image = $file;
                            $member->update();
                        }
                        else {
                            $member = new Question();
                            $member->get($id);
                            $member->delete();
                        }

                        if (MessageHandler::countError() == 0)
                            header("Location: ".SITE_URL.'?c=GetThePicture');
                    }
                    
                }
                    
                
                
                   
                    
            }

            $this->vars['question'] = $member;
            
            $this->display('GetThePicture_form');
        }
        
        function editQuestion() {
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            if (!isset($_GET['id']))
                header("Location: ".SITE_URL.'?c=GetThePicture');
            
            $id = $_GET['id'];
            
            $cfer = new Cfer(array(
            _LB_QUESTION => SITE_URL.'?c=GetThePicture',
            _LB_ADD_QUESTION => SITE_URL.'?c=GetThePicture&a=editQuestion&id='.$id));
            
            $this->vars['section'] = "GetThePicture";
            $this->vars['title'] = "Update an existing question of 'Get The Picture'";
            $this->vars['backlink'] = SITE_URL.'?c=GetThePicture';
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $member = new Question();
            $member->get($id);
            
            if ($act == ACT_VALID_EDIT) {
                
                $member->title = isset($_POST['title']) ? $_POST['title'] : '';
                $member->question_type_id = 1;
                
                if ($_FILES["image"]["name"]) {
                    if ($file = $this->uploadPicture("1_".$id, true)) {
                        $member->image = $file;
                    }
                }
                
                if ($member->validateInput()) {
                    if ($member->update()) {
                        header("Location: ".SITE_URL.'?c=GetThePicture');
                    }
                }
                
            }

            $this->vars['question'] = $member;
            
            $this->display('GetThePicture_form');
        }
        
        function deleteQuestion() {
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            if (!isset($_GET['id']))
                header("Location: ".SITE_URL.'?c=GetThePicture');
            
            $id = $_GET['id'];
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $member = new Question();
            
            if (!$member->get($id))
                header("Location: ".SITE_URL.'?c=GetThePicture');
            
            $member->delete();
            
            header("Location: ".SITE_URL.'?c=GetThePicture');
        }
        
        function addAnswer() {
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            $cfer = new Cfer(array(
            _LB_QUESTION => SITE_URL.'?c=GetThePicture',
            _LB_ADD_ANSWER => SITE_URL.'?c=GetThePicture&a=addAnswer'));
            
            $this->vars['section'] = "GetThePicture";
            $this->vars['title'] = "Add an answer of a question of 'Get The Picture'";
            $this->vars['backlink'] = SITE_URL.'?c=GetThePicture';
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $member_id = null;
            
            if (isset($_REQUEST['question_id']))
                $member_id = $_REQUEST['question_id'];
            
            $answer = new Answer();
            
            if ($act == ACT_VALID_EDIT) {
                
                if (!isset($member_id))
                    MessageHandler::add ('Missing question ID');
                
                $answer->content = isset($_POST['content']) ? $_POST['content'] : '';
                $answer->question_id = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : null;
                $member_id = $answer->question_id;
                $answer->is_correct = isset($_POST['is_correct']) ? 1 : 0;
                
                if (MessageHandler::countError() == 0) {
                    if ($answer->validateInput()) {
                        $id = $answer->insert();
                        if ($id)
                            header("Location: ".SITE_URL.'?c=GetThePicture');
                    }
                }
                
            }
            
            $member = new Question();
            $member->addSelect();
            $member->addSelect("question.id, question.title");
            $member->addWhere("question.question_type_id = 1");
            $member->orderBy("question.title ASC");
            $member->find();

            $this->vars['question'] = $member;
            $this->vars['answer'] = $answer;
            $this->vars['question_id'] = $member_id;
            
            $this->display('GetThePicture_Answer_form');
        }
        
        function editAnswer() {
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            $cfer = new Cfer(array(
            _LB_QUESTION => SITE_URL.'?c=GetThePicture',
            _LB_ADD_ANSWER => SITE_URL.'?c=GetThePicture&a=editAnswer'));
            
            $this->vars['section'] = "GetThePicture";
            $this->vars['title'] = "Edit an existing answer of a question of 'Get The Picture'";
            $this->vars['backlink'] = SITE_URL.'?c=GetThePicture';
            
            if (!isset($_GET['id']))
                header("Location: ".SITE_URL.'?c=GetThePicture');
            
            $id = $_GET['id'];
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $member_id = null;
            
            if (isset($_REQUEST['question_id']))
                $member_id = $_REQUEST['question_id'];
            
            $answer = new Answer();
            
            if (!$answer->get($id))
                header("Location: ".SITE_URL.'?c=GetThePicture');
            
            $member_id = $answer->question_id;
            
            if ($act == ACT_VALID_EDIT) {
                
                if (!isset($member_id))
                    MessageHandler::add ('Missing question ID');
                
                $answer->content = isset($_POST['content']) ? $_POST['content'] : '';
                $answer->question_id = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : null;
                $member_id = $answer->question_id;
                $answer->is_correct = isset($_POST['is_correct']) ? 1 : 0;
                
                if ($answer->validateInput()) {
                    if ($answer->update())
                        header("Location: ".SITE_URL.'?c=GetThePicture');
                }
                
            }
            
            $member = new Question();
            $member->addSelect();
            $member->addSelect("question.id, question.title");
            $member->addWhere("question.question_type_id = 1");
            $member->orderBy("question.title ASC");
            $member->find();

            $this->vars['question'] = $member;
            $this->vars['answer'] = $answer;
            $this->vars['question_id'] = $member_id;
            
            $this->display('GetThePicture_Answer_form');
        }
        
        function deleteAnswer() {
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            if (!isset($_GET['id']))
                header("Location: ".SITE_URL.'?c=GetThePicture');
            
            $id = $_GET['id'];
            
            $answer = new Answer();
            
            if (!$answer->get($id))
                header("Location: ".SITE_URL.'?c=GetThePicture');
            
            $answer->delete();
            
            header("Location: ".SITE_URL.'?c=GetThePicture');
        }
        
        function setCorrect() {
            $answer = new Answer();
            
            if (!isset($_GET['id']))
                header("Location: ".SITE_URL.'?c=GetThePicture');
            
            $id = $_GET['id'];
            
            if (!$answer->get($id))
                header("Location: ".SITE_URL.'?c=GetThePicture');
            
            $member_id = $answer->question_id;
            $answer->is_correct = 1;
            $answer->update();
            
            $answer = new Answer();
            $answer->query("UPDATE answer SET is_correct = 0 WHERE question_id = $member_id AND id <> $id");
            
            header("Location: ".SITE_URL.'?c=GetThePicture');
        }
        
}

